package com.example.administrator.u17.activity;

import android.graphics.Color;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;

import com.example.administrator.u17.R;
import com.example.administrator.u17.adapter.MineFragmentAdapter;
import com.example.administrator.u17.fragment.ReadingTilketFragment;
import com.example.administrator.u17.fragment.YTicketJLFragment;
import com.example.administrator.u17.fragment.YaoQiJLFragment;


import java.util.ArrayList;
import java.util.List;

public class YticketActivity extends AppCompatActivity implements TabLayout.OnTabSelectedListener {
    // TODO: 2016/11/15 消费记录

    private TabLayout tabLayout;
    private String[] titles;
    private ViewPager viewPager;
    private List<Fragment> fragmentList;
    private MineFragmentAdapter mineFragmentAdapter;
    private ImageView ivBack;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_yticket);
        initView();
        initData();
        setData();
        setListener();
    }

    private void setListener() {

        tabLayout.addOnTabSelectedListener(this);
    }

    private void setData() {
        viewPager.setAdapter(mineFragmentAdapter);

        tabLayout.setupWithViewPager(viewPager);

        tabLayout.setTabTextColors(Color.GRAY, Color.BLACK);
        tabLayout.setSelectedTabIndicatorColor(Color.BLACK);
        tabLayout.setSelectedTabIndicatorHeight(5);

    }

    private void initData() {

        fragmentList.add(new YaoQiJLFragment());
        fragmentList.add(new YTicketJLFragment());
        fragmentList.add(new ReadingTilketFragment());

    }

    private void initView() {
        titles = getResources().getStringArray(R.array.mine_array);
        fragmentList = new ArrayList<>();
        viewPager = (ViewPager) findViewById(R.id.viewPager_mine_replace);
        mineFragmentAdapter = new MineFragmentAdapter(getSupportFragmentManager(),fragmentList, titles);
        tabLayout = (TabLayout) findViewById(R.id.tab_mine_detail);
        ivBack= (ImageView) findViewById(R.id.iv_mine_back);
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }


    @Override
    public void onTabSelected(TabLayout.Tab tab) {
        for (int i = 0; i <tabLayout.getTabCount() ; i++) {
            if (tabLayout.getTabAt(i)==tab){

                viewPager.setCurrentItem(i);

                break;

            }
        }
    }

    @Override
    public void onTabUnselected(TabLayout.Tab tab) {

    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {

    }
}
